﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using Entity;

namespace DAL
{
    public class ContactDAL : SqlDataProvider
    {
        #region[Contact_GetById]
        public List<Contact> Contact_GetById(string Id)
        {
            List<Entity.Contact> list = new List<Entity.Contact>();
            using (SqlCommand dbCmd = new SqlCommand("sp_Contact_GetById", GetConnection()))
            {
                dbCmd.CommandType = CommandType.StoredProcedure;
                dbCmd.Parameters.Add(new SqlParameter("@Id", Id));
                SqlDataReader dr = dbCmd.ExecuteReader();
                if (dr.HasRows)
                {
                    while (dr.Read())
                    {
                        list.Add(Contact.ContactIDataReader(dr));
                    }
                }
                dr.Close();
            }
            return list;
        }
        #endregion
        #region[Contact_GetByTop]
        public List<Contact> Contact_GetByTop(string Top, string Where, string Order)
        {
            List<Entity.Contact> list = new List<Entity.Contact>();
            using (SqlCommand dbCmd = new SqlCommand("sp_Contact_GetByTop", GetConnection()))
            {
                dbCmd.CommandType = CommandType.StoredProcedure;
                dbCmd.Parameters.Add(new SqlParameter("@Top", Top));
                dbCmd.Parameters.Add(new SqlParameter("@Where", Where));
                dbCmd.Parameters.Add(new SqlParameter("@Order", Order));
                SqlDataReader dr = dbCmd.ExecuteReader();
                if (dr.HasRows)
                {
                    while (dr.Read())
                    {
                        list.Add(Contact.ContactIDataReader(dr));
                    }
                }
                dr.Close();
            }
            return list;
        }
        #endregion
        #region[Contact_GetByAll]
        public List<Contact> Contact_GetByAll(string Lang)
        {
            List<Entity.Contact> list = new List<Entity.Contact>();
            using (SqlCommand dbCmd = new SqlCommand("sp_Contact_GetByAll", GetConnection()))
            {
                dbCmd.CommandType = CommandType.StoredProcedure;
                dbCmd.Parameters.Add(new SqlParameter("@Lang", Lang));
                SqlDataReader dr = dbCmd.ExecuteReader();
                if (dr.HasRows)
                {
                    while (dr.Read())
                    {
                        list.Add(Contact.ContactIDataReader(dr));
                    }
                }
                dr.Close();
            }
            return list;
        }
        #endregion
        #region[Contact_Paging]
        public List<Contact> Contact_Paging(string CurentPage, string PageSize, string Lang)
        {
            List<Entity.Contact> list = new List<Entity.Contact>();
            using (SqlCommand dbCmd = new SqlCommand("sp_Contact_Paging", GetConnection()))
            {
                dbCmd.CommandType = CommandType.StoredProcedure;
                dbCmd.Parameters.Add(new SqlParameter("@CurentPage", CurentPage));
                dbCmd.Parameters.Add(new SqlParameter("@PageSize", PageSize));
                dbCmd.Parameters.Add(new SqlParameter("@Lang", Lang));
                SqlDataReader dr = dbCmd.ExecuteReader();
                if (dr.HasRows)
                {
                    while (dr.Read())
                    {
                        list.Add(Contact.ContactIDataReader(dr));
                    }
                }
                dr.Close();
            }
            return list;
        }
        #endregion
        #region[Contact_Insert]
        public bool Contact_Insert(Contact data)
        {
            using (SqlCommand dbCmd = new SqlCommand("sp_Contact_Insert", GetConnection()))
            {
                dbCmd.CommandType = CommandType.StoredProcedure;
                dbCmd.Parameters.Add(new SqlParameter("@Name", data.Name));
                dbCmd.Parameters.Add(new SqlParameter("@Company", data.Company));
                dbCmd.Parameters.Add(new SqlParameter("@Address", data.Address));
                dbCmd.Parameters.Add(new SqlParameter("@Tel", data.Tel));
                dbCmd.Parameters.Add(new SqlParameter("@Mail", data.Mail));
                dbCmd.Parameters.Add(new SqlParameter("@Detail", data.Detail));
                dbCmd.Parameters.Add(new SqlParameter("@Date", data.Date));
                dbCmd.Parameters.Add(new SqlParameter("@Active", data.Active));
                dbCmd.Parameters.Add(new SqlParameter("@Lang", data.Lang));
                dbCmd.ExecuteNonQuery();
            }
            //Clear cache
            System.Web.HttpContext.Current.Cache.Remove("Contact");
            return true;
        }
        #endregion
        #region[Contact_Update]
        public bool Contact_Update(Contact data)
        {
            using (SqlCommand dbCmd = new SqlCommand("sp_Contact_Update", GetConnection()))
            {
                dbCmd.CommandType = CommandType.StoredProcedure;
                dbCmd.Parameters.Add(new SqlParameter("@Id", data.Id));
                dbCmd.Parameters.Add(new SqlParameter("@Name", data.Name));
                dbCmd.Parameters.Add(new SqlParameter("@Company", data.Company));
                dbCmd.Parameters.Add(new SqlParameter("@Address", data.Address));
                dbCmd.Parameters.Add(new SqlParameter("@Tel", data.Tel));
                dbCmd.Parameters.Add(new SqlParameter("@Mail", data.Mail));
                dbCmd.Parameters.Add(new SqlParameter("@Detail", data.Detail));
                dbCmd.Parameters.Add(new SqlParameter("@Date", data.Date));
                dbCmd.Parameters.Add(new SqlParameter("@Active", data.Active));
                dbCmd.Parameters.Add(new SqlParameter("@Lang", data.Lang));
                dbCmd.ExecuteNonQuery();
            }
            //Clear cache
            System.Web.HttpContext.Current.Cache.Remove("Contact");
            return true;
        }
        #endregion
        #region[Contact_Delete]
        public bool Contact_Delete(string Id)
        {
            using (SqlCommand dbCmd = new SqlCommand("sp_Contact_Delete", GetConnection()))
            {
                dbCmd.CommandType = CommandType.StoredProcedure;
                dbCmd.Parameters.Add(new SqlParameter("@Id", Id));
                dbCmd.ExecuteNonQuery();
            }
            //Clear cache
            System.Web.HttpContext.Current.Cache.Remove("Contact");
            return true;
        }
        #endregion

    }
}
